package com.swcw.platform.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

@SuppressWarnings("deprecation")
public abstract class HttpTool {

	private static final int connectionTimeOut = 3000;

	private static final int soTimeout = 40000;

	private static Logger logger = Logger.getLogger(HttpTool.class);

	/**
	 * 将数据流写入文件
	 *
	 * @param formFile
	 *            从页面传过来的文件对象
	 * @param file
	 *            文件对象
	 */
	public static void streamToFile(MultipartFile filedate, File file) {

		OutputStream out = null;
		InputStream in = null;

		try {
			out = new FileOutputStream(file);
			in = filedate.getInputStream();
			int bytesRead;

			byte[] buffer = new byte[8192];

			while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
				out.write(buffer, 0, bytesRead);
			}
		} catch (FileNotFoundException fnfe) {

		} catch (IOException ioe) {
		} finally {
			close(out);
			close(in);
		}
	}

	/**
	 * 文件流
	 *
	 * @param out
	 *            写文件流
	 * @param in
	 *            读文件流
	 * @see [类、类#方法、类#成员]
	 */
	public static void close(OutputStream out) {
		try {
			if (null != out) {
				out.flush();

				out.close();
			}
		} catch (IOException io) {
		}
	}

	/**
	 * 关闭流
	 *
	 * @param in
	 *            in
	 */
	public static void close(InputStream in) {
		try {
			if (null != in) {
				in.close();
			}
		} catch (IOException e) {
		}
	}

	/**
	 * post消息到服务器
	 * 
	 * @param serverUrl
	 *            服务器地址
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public static String postUrl(String serverUrl, String content) {

		// 如果服务url为空，直接返回空
		if (null == serverUrl || 0 == serverUrl.length()) {
			return null;
		}

		BufferedReader br = null;
		HttpURLConnection urlCon = null;
		BufferedWriter bw = null;
		String result;
		try {
			URL url = new URL(serverUrl);
			urlCon = (HttpURLConnection) url.openConnection();
			urlCon.setRequestMethod("POST");
			urlCon.setDoInput(true);
			urlCon.setDoOutput(true);
			bw = new BufferedWriter(new OutputStreamWriter(urlCon.getOutputStream(), "utf-8"));
			bw.write(content);
			bw.flush();
			br = new BufferedReader(new InputStreamReader(urlCon.getInputStream(), "utf-8"));
			result = read(br);
		} catch (Exception e) {
			throw new RuntimeException("post message to server error!url " + serverUrl, e);
		} finally {
			if (null != br) {
				try {
					br.close();
				} catch (IOException e) {
				}
			}
			if (null != bw) {
				try {
					bw.close();
				} catch (IOException e) {
				}
			}
			if (null != urlCon) {
				try {
					urlCon.disconnect();
				} catch (Exception e) {
				}
			}
		}
		return result;
	}
	
	public static boolean setSms(String mobile,String msg){
		Map<String,Object> map = new HashMap<>();
		map.put("mailId", mobile);
		return sendPost(map,"http://61.160.159.12:6633/sms/sendMail").indexOf("M01") != -1;
	}
	
	public static void main(String[] args) {
		Map<String,Object> map = new HashMap<>();
		map.put("systemId", "");
		map.put("userId", "");
		System.out.println(sendPost(map,"http://132.224.244.64:6633/sms/sendUser"));
	}
	
	@SuppressWarnings("unchecked")
	public static String sendPost(Map<String,Object> map,String url) {
		DataOutputStream out = null;
        BufferedReader responseReader = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            HttpURLConnection  connection = (HttpURLConnection) realUrl.openConnection();
            // 设置通用的请求属性
            connection.setConnectTimeout(2000);// 设置连接超时时间，单位毫秒  
            connection.setReadTimeout(2000);// 设置读取数据超时时间，单位毫秒  
            connection.setDoOutput(true);// 是否打开输出流 true|false  
            connection.setDoInput(true);// 是否打开输入流true|false  
            connection.setRequestMethod("POST");// 提交方法POST|GET  
            connection.setUseCaches(false);// 是否缓存true|false
            connection.setRequestProperty("Accept-Charset", "UTF-8");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            // 发送POST请求必须设置如下两行
            connection.setDoOutput(true);
            connection.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new DataOutputStream(connection.getOutputStream());
            // 发送请求参数
            StringBuffer str = new StringBuffer();  
            Set<String> keys = map.keySet();
            for(String key : keys){
            	if("channelIds".equals(key)){
            		List<String> strs = (List<String>) map.get(key);
            		for (String string : strs) {
            			str.append(key).append("=").append(string).append("&");
					}
            		continue;
            	}
            	str.append(key).append("=").append(map.get(key)).append("&");
            }
            out.write(str.toString().getBytes("UTF-8"));
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            // 当正确响应时处理数据   
	        StringBuffer sb = new StringBuffer();   
	        String readLine;   
	        // 处理响应流，必须与服务器响应流输出的编码一致   
	        responseReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));   
	        while ((readLine = responseReader.readLine()) != null) 
	        {   
	        	sb.append(readLine).append("\n");   
	        }   
	        result = sb.toString(); 
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！"+e.getMessage());
            //e.printStackTrace();
            return "{\"result\":\"M03\",\"msg\":\""+e.getMessage()+"\"}";
        }
        //使用finally块来关闭输出流、输入流
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(responseReader!=null){
                	responseReader.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }  

	/**
	 * 读响应码
	 * 
	 * @param br
	 *            输入流
	 * @return 响应码
	 * @see [类、类#方法、类#成员]
	 */
	public static String read(BufferedReader br) {
		StringBuilder buffer = new StringBuilder();
		try {
			String line = null;
			while (null != (line = br.readLine())) {
				buffer.append(line);
			}
		} catch (Exception e) {
			throw new RuntimeException("send short message error", e);
		}

		return buffer.toString();
	}

	/**
	 * 发送消息到服务器
	 * 
	 * @param serverUrl
	 *            服务器地址
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public static String sendUrl(String serverUrl) {

		String result = sendUrl(serverUrl, "utf-8");

		return result;
	}
	
	
	/**
	 * 发送消息到服务器
	 * 
	 * @param serverUrl
	 *            服务器地址
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public static String sendUrlCode(String serverUrl) {

		String result = sendUrl(serverUrl, "GBK");

		return result;
	}

	/**
	 * 发送消息到服务器
	 * 
	 * @param serverUrl
	 *            服务器地址
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public static String sendUrl(String serverUrl, String charsetName) {

		BufferedReader br = null;
		HttpURLConnection urlCon = null;
		String result;
		try {
			URL url = new URL(serverUrl);
			urlCon = (HttpURLConnection) url.openConnection();
			urlCon.setRequestMethod("GET");
			br = new BufferedReader(new InputStreamReader(urlCon.getInputStream(), charsetName));
			result = read(br);
		} catch (Exception e) {
			throw new RuntimeException("send request message error", e);
		} finally {
			if (null != br) {
				try {
					br.close();
				} catch (IOException e) {
				}
			}
			if (null != urlCon) {
				try {
					urlCon.disconnect();
				} catch (Exception e) {
				}
			}
		}
		return result;
	}

	@SuppressWarnings("resource")
	public static String post2(String actionUrl, Map<String, String> params) {
		BasicHttpParams bp = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(bp, connectionTimeOut); // 超时时间设置
		HttpConnectionParams.setSoTimeout(bp, soTimeout);
		HttpClient httpclient = new DefaultHttpClient(bp);
		HttpPost httpPost = new HttpPost(actionUrl);
		List<NameValuePair> list = new ArrayList<NameValuePair>();
		for (Map.Entry<String, String> entry : params.entrySet()) {// 构建表单字段内容
			String key = entry.getKey();
			String value = entry.getValue();
			list.add(new BasicNameValuePair(key, value));
		}
		HttpResponse httpResponse;
		String responseString = "";
		logger.warn("传入后台的参数：" + list);
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));
			httpResponse = httpclient.execute(httpPost);
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				responseString = EntityUtils.toString(httpResponse.getEntity());
				return responseString;
			} else if (httpResponse.getStatusLine().getStatusCode() == 404) {
				logger.warn("actionUrl:{} not found 404!" + actionUrl);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			httpclient.getConnectionManager().shutdown();
		}
		return null;
	}

	@SuppressWarnings("resource")
	public static String post(String actionUrl, Map<String, Object> params) {
		BasicHttpParams bp = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(bp, connectionTimeOut); // 超时时间设置
		HttpConnectionParams.setSoTimeout(bp, soTimeout);
		HttpClient httpclient = new DefaultHttpClient(bp);
		HttpPost httpPost = new HttpPost(actionUrl);
		List<NameValuePair> list = new ArrayList<NameValuePair>();
		for (Map.Entry<String, Object> entry : params.entrySet()) {// 构建表单字段内容
			String key = entry.getKey();
			String value = entry.getValue().toString();
			list.add(new BasicNameValuePair(key, value));
		}
		HttpResponse httpResponse;
		String responseString = "";
		logger.warn("-----HttpClientHelper4TelephoneFee-----" + list);
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));
			httpResponse = httpclient.execute(httpPost);
			logger.warn("-----httpResponse-----" + httpResponse);
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				responseString = EntityUtils.toString(httpResponse.getEntity());
				return responseString;
			} else if (httpResponse.getStatusLine().getStatusCode() == 404) {
				logger.warn("actionUrl:{} not found 404!" + actionUrl);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			httpclient.getConnectionManager().shutdown();
		}
		return null;
	}

	/**
	 * 生成excel文件
	 * 
	 * @param headList
	 *            excel文件头
	 * @param contentList
	 *            excel文件的内容
	 * @param fileName
	 *            目标文件名(如文件名为null，则自动生成一个文件名)
	 * @return 保存的文件名
	 */
	public static Workbook exportWithExcelFile(List<String> headList, List<List<String>> contentList) {
		if (headList == null || contentList == null) {
			return new SXSSFWorkbook(100);
		}

		Workbook workbook = new SXSSFWorkbook(100);// 最重要的就是使用SXSSFWorkbook，表示流的方式进行操作
		Sheet sheet = workbook.createSheet();

		CellStyle style = workbook.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

		// 设置表头
		Row writeRow = sheet.createRow(0);
		for (Integer headIndex = 0; headIndex < headList.size(); headIndex++) {
			Cell cell = writeRow.createCell(headIndex);
			cell.setCellValue(headList.get(headIndex));
			cell.setCellStyle(style);
		}

		// 写入实体数据
		for (Integer rowIndex = 0; rowIndex < contentList.size(); rowIndex++) {
			Row contentRow = sheet.createRow(rowIndex + 1);
			List<String> cList = contentList.get(rowIndex);
			for (Integer cellIndex = 0; cellIndex < cList.size(); cellIndex++) {
				Cell cell = contentRow.createCell(cellIndex);
				cell.setCellValue(cList.get(cellIndex));
				cell.setCellStyle(style);
			}
		}
		return workbook;
	}

	/**
	 * 将数据流写入文件
	 * 
	 * @param conn
	 *            从页面传过来的文件对象
	 * @param file
	 *            文件对象
	 */
	public static void streamToFile(HttpURLConnection conn, File file) throws Exception {

		OutputStream out = null;
		InputStream in = null;

		try {
			out = new FileOutputStream(file);
			in = conn.getInputStream();
			int bytesRead;

			byte[] buffer = new byte[8192];

			while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
				out.write(buffer, 0, bytesRead);
			}
		} catch (FileNotFoundException fnfe) {
			fnfe.printStackTrace();
		} finally {
			close(out);
			close(in);
		}
	}
	
	/**
	 * 获取真实的IP4地址
	 * 
	 * @param request
	 * @return
	 */
	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

}
